/*
 ============================================================================
 Name		: TStringItem.cpp
 Author	  : wangli@opi-corp.com
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : TStringItem implementation
 ============================================================================
 */

#include "TStringItem.h"
#include "Service.h"

TStringItem* TStringItem::NewL(const TDesC& aString)
{
    TUint length = (aString.Length() > 0)? aString.Length() : 1;
    __ASSERT_DEBUG(sizeof(TItem) == 4, User::Invariant());
    TAny* result = User::AllocL(sizeof(TItem) + length*sizeof(TUint16));
    return ::new(result) TStringItem(aString);
}

TStringItem::TStringItem(const TDesC& aString)
    :TItem(TStringItemType)
{
    iSize = aString.Length() * sizeof(TUint16);
    memcpy(iString, aString.Ptr(), iSize);
}

TStringItem::~TStringItem()
{
}


TDesC* TStringItem::toString()const
{
    TUint length = iSize/sizeof(TUint16);
    HBufC* result = HBufC::NewL(length);
    result->Des().Append(iString, length);
    return result;
}

TBool TStringItem::Equal(const TItem& aItem)const
{
    TBool base = this->TItem::Equal(aItem);
    if(!base)
        return EFalse;
    
    const TStringItem& item = static_cast<const TStringItem&>(aItem);
    const TUint16* pItem = item.iString;
    const TUint16* pThis = this->iString;
    for(TUint index = 0; index < iSize/sizeof(TUint16); ++index)
    {
        if(pItem[index] != pThis[index])
            return EFalse;
    }
    return ETrue;
}
